package com.vip.zuul.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vip.zuul.configuration.VIPZuulRouteLocator;
import com.vip.zuul.service.IVipRouteService;
import com.vip.zuul.utils.RedisUtils;
import com.vip.zuul.vo.RouteDTO;
import com.vip.zuul.vo.RouteReqVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description : 
 * @Author Evan Liu
 * @Date 2018年11月12日
 */
@Service
public class VipRouteServiceImpl implements IVipRouteService{
    @Autowired
    VIPZuulRouteLocator vipRouteLocator;
    
    
    private static String ZUUL_NGINX_ROUTE_RULES = "ZUUL_NGINX_ROUTE_RULES";

	@Override
	public List<RouteDTO> getRouteRules() {
		Map<Object, Object> routes = RedisUtils.hGetAll(ZUUL_NGINX_ROUTE_RULES);
		return null;
	}
	
	@Override
	public boolean addRouteRules(RouteDTO entity) {
		//refreshRoute();
		return false;
	}

	@Override
	public boolean removeRouteRules(String ruleid) {
		boolean result = false;
		List<RouteDTO> rules = getRouteRules();
		for (RouteDTO vipZuulRouteRules : rules) {
			if(StringUtils.equalsIgnoreCase(vipZuulRouteRules.getZuulRoute().getId(), ruleid)) {
				rules.remove(vipZuulRouteRules);
				result = true;
			}
		}
		//refreshRoute();
		return result;
	}
	
	@Override
    public void refreshRoute() {
        //RoutesRefreshedEvent routesRefreshedEvent = new RoutesRefreshedEvent(routeLocator);
        //publisher.publishEvent(routesRefreshedEvent);
        vipRouteLocator.refresh();
        //当设置为ServiceID 需要重新配置Ribbon
        vipRouteLocator.configRibbonWithServiceID();
    }
}


